<?php

namespace app\api\validate\user;

use app\common\model\user\User;
use tmcore\base\BaseValidate;
use tmcore\utils\Helper;

class UserValidate extends BaseValidate
{
    protected $rule = [
        'id' => 'require',
        'mobile' => 'require|checkUpdateMobile',
        'code' => 'require',
        'password' => 'require',
        'new_password' => 'require|checkUpdatePassword',
        'avatar' => 'require',
        'nickname' => 'require',
        'email' => 'require',
        'sex' => 'require',
    ];

    protected $message = [
        'id.require' => '请输入用户id',
        'mobile.require' => '请输入手机号',
        'code.require' => '请输入验证码',
        'password.require' => '请输入密码',
        'new_password.require' => '请输入新密码',
        'avatar.require' => '请输入头像',
        'nickname.require' => '请输入昵称',
        'email.require' => '请输入邮箱',
        'sex.require' => '请输入性别',
    ];

    /**
     * 获取用户信息检验
     * @return UserValidate
     */
    public function sceneUserInfoCheck()
    {
        return $this->only(['id']);
    }

    /**
     * 获取修改手机号检验
     * @return UserValidate
     */
    public function sceneUpdateMobileCheck()
    {
        return $this->only(['id', 'mobile', 'code'])->append('mobile', 'checkUpdateMobile');
    }

    /**
     * 获取修改密码检验
     * @return UserValidate
     */
    public function sceneUpdatePasswordCheck()
    {
        return $this->only(['id', 'password', 'new_password'])->append('password', 'checkUpdatePassword');
    }

    /**
     * 获取头像检验
     * @return UserValidate
     */
    public function sceneUpdateAvatarCheck()
    {
        return $this->only(['id', 'avatar']);
    }

    /**
     * 获取昵称检验
     * @return UserValidate
     */
    public function sceneUpdateNicknameCheck()
    {
        return $this->only(['id', 'nickname']);
    }

    /**
     * 获取邮箱
     * @return UserValidate
     */
    public function sceneUpdateEmailCheck()
    {
        return $this->only(['id', 'email']);
    }

    /**
     * 获取性别检验
     * @return UserValidate
     */
    public function sceneUpdateSexCheck()
    {
        return $this->only(['id', 'sex']);
    }

    /**
     * 检查密码
     * @param $value
     * @param $rule
     * @param $data
     * @return bool|string
     */
    public function checkUpdatePassword($value, $rule, $data) {
        // 6-16位，同时包含字母、数字、特殊符号
        if(!Helper::check_password($value)) return '密码必须6-16位，同时包含字母、数字、特殊符号';

        $user = User::where(['id' => $data['id']])->field('id,mobile,password,status')->findOrEmpty();
        if(!$user) return '找不到用户信息';
        if($user['status'] != 1) return '用户状态异常,不可操作';
        if(User::getPassword($data['password'], 'user_register') != $user['password']) return '原始密码有误';
        if(User::getPassword($data['new_password'], 'user_register') == $user['password']) return '新密码不可和原始密码相同';

        return true;
    }

    /**
     * 检查手机号
     * @param $value
     * @param $rule
     * @param $data
     * @return bool|string
     */
    public function checkUpdateMobile($value, $rule, $data) {
        if(!Helper::check_mobile($value)) return '手机号不符合要求';

        $user = User::where(['id' => $data['id']])->field('id,mobile,status')->findOrEmpty();
        if(!$user) return '找不到用户信息';
        if($user['status'] != 1) return '用户状态异常,不可操作';
        if($user['mobile'] && $value == $user['mobile']) return '修改手机号不可和原手机号相同';

        return true;
    }
}